package com.zyx.scheduled.runner;

import cn.hutool.core.date.DateUtil;
import com.zyx.scheduled.config.DefaultSchedulingConfigurer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>初始化定时任务
 *
 * @author Yaxi.Zhang
 * @since 2022/11/8 00:38
 */
@Slf4j
@Component
@DependsOn("defaultSchedulingConfigurer")
public class SchedulingCommandLineRunner implements CommandLineRunner {

    @Resource
    private DefaultSchedulingConfigurer defaultSchedulingConfigurer;

    @Override
    public void run(String... args) throws Exception {
        log.info("command run init task: [{}]", "register");
        defaultSchedulingConfigurer.addTriggerTask("task", new TriggerTask(
                () -> log.info("scheduled task: [{}]", DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss")),
                new CronTrigger("0/5 * * * * ?")));
    }
}
